package com.zzh.htts.voice;


import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.audiofx.NoiseSuppressor;
import android.os.Build;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationChannelCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import com.zzh.lib.htts.HTTSLib;
import com.zzh.lib.recorder.HMediaRecorderParams;
import com.zzh.lib.recorder.def.IRecorder;
import com.zzh.lib.recorder.def.OnRecorderProcessListener;
import com.zzh.lib.recorder.pcm.PCMRecorder;

import java.util.Arrays;

/**
 * Created by zzh on 2024/12/16.
 *
 * @date: 2024/12/16 16:26
 * @email: zzh_hz@126.com
 * @author: zzh
 * @description: com.zzh.htts.voice
 */
public class RecordService extends Service {

    RecordService mInstance;
    IRecorder recorder;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        startForegroundV3();
        Log.d("--pcm", "onCreate: " + System.currentTimeMillis());
    }

    long dot = System.currentTimeMillis();

    private IRecorder initPCMRecorder() {
        HMediaRecorderParams params = HMediaRecorderParams.buildDefaultPCM();
        params.setAudioEncoder(4000);
        params.setSave2File(true);
        params.setDenoise(true);
        params.setFileFormatWave(true);
        params.setFileSaveDir(getCacheDir().getAbsolutePath());
        params.setOnRecorderProcessListener(new OnRecorderProcessListener() {

            @Override
            public void onBeforeInitAudioRecord(IRecorder recorder) {

            }

            @Override
            public void onProcess(PCMRecorder recorder, byte[] buffer, int len) {

            }

            @Override
            public void volumeChange(double volume) {

                if ((System.currentTimeMillis() - dot) > 300) {
                    int voiceSize = (int) (volume * 6);
                    Log.d("--pcm", "pcm 音量：" + voiceSize);
                    dot = System.currentTimeMillis();

                }

            }

            @Override
            public void onStart(PCMRecorder recorder) {
                Log.d("--pcm", "pcm 开始录制: " + recorder.getRecordFile().getAbsolutePath());
            }

            @Override
            public void onEnd(long duration) {
                Log.d("--pcm", "pcm 录制结束: " + duration);

            }
        });

        return HTTSLib.getInstance().setRecorderParams(params).create();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("--pcm", "onStartCommand: " + System.currentTimeMillis());

        if (recorder == null) {
            recorder = initPCMRecorder();
        }
        try {
            String recordType = intent.getStringExtra("RECORD_TYPE");
            if (TextUtils.equals("release", recordType)) {
                recorder.stop();
                recorder.release();
                recorder = null;
            } else {
                if (recorder.isRecorder()) {
                    recorder.stop();
                } else {
                    recorder.start();
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return START_NOT_STICKY;
    }

    private void startForegroundV3() {

        createNotificationChannel();
    }

    private void createNotificationChannel() {
        CharSequence name = "语音录制";
        String description = "唱歌";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;

        NotificationChannelCompat channel = new NotificationChannelCompat.Builder("record_voice", importance)
                .setName(name)
                .setDescription(description)
                .build();

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        notificationManager.createNotificationChannel(channel);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(getBaseContext(), "record_voice");
        builder.setSmallIcon(R.mipmap.ic_launcher);

        startForeground(2003, builder.build());
    }
}
